<template>
  <div class="ShoppingCar">
    <div class="Home_list1">
      <ul>
        <li v-for="item in Home_list1" :key="item">
          <a>{{ item }}</a>
        </li>
      </ul>
    </div>

    <div class="ShoppingCar_nav">
      <div><img src="@/assets/logo1.png" alt="网站logo" /></div>
      <div class="ShoppingCar_h3"><h3>购物车</h3></div>
      <div class="ShoppingCar_p">
        <p>
          商品从门店或者发货，我们将尽快发货。门店自提订单，24小时内将提货码发送至您的手机，请您留意手机短信。由于市场变化及各种合理商业努力都难以控制的
          因素的影响，商城无法保证用户提交的订单希望购买的商品都有货；如用户拟购买的商品缺货，用户和商家都可取消订单。
        </p>
      </div>
    </div>

    <div class="cart-container">
      <div v-if="cartItems.length === 0" class="empty-cart">
        <img src="@/assets/gwc.png" alt="空购物车" class="empty-image" />
        <p>您的购物车还是空的</p>
        <button @click="$router.push('/')" class="btn-shopping">去逛逛</button>
      </div>

      <div v-else>
        <div class="cart-header">
          <div class="header-select">
            <el-checkbox v-model="allSelected" @change="toggleAllSelect"
              >全选</el-checkbox
            >
          </div>
          <div class="header-info">商品信息</div>
          <div class="header-price">单价</div>
          <div class="header-quantity">数量</div>
          <div class="header-subtotal">小计</div>
          <div class="header-action">操作</div>
        </div>

        <div class="cart-list">
          <div v-for="item in cartItems" :key="item.id" class="cart-item">
            <div class="item-select">
              <el-checkbox
                v-model="item.selected"
                @change="toggleSelect(item.id)"
              ></el-checkbox>
            </div>
            <div class="item-info">
              <img :src="item.image" :alt="item.name" class="item-image" />
              <div class="item-details">
                <h4 class="item-name">{{ item.name }}</h4>
                <p class="item-spec">{{ item.spec }}</p>
              </div>
            </div>
            <div class="item-price">¥{{ item.price.toFixed(2) }}</div>
            <div class="item-quantity">
              <el-input-number
                v-model="item.quantity"
                :min="1"
                size="small"
                @change="updateQuantity(item)"
              ></el-input-number>
            </div>
            <div class="item-subtotal">
              ¥{{ (item.price * item.quantity).toFixed(2) }}
            </div>
            <div class="item-action">
              <el-button
                type="text"
                icon="el-icon-delete"
                @click="removeItem(item)"
              ></el-button>
            </div>
          </div>
        </div>

        <div class="cart-footer">
          <div class="footer-left">
            <el-button @click="clearCart" icon="el-icon-delete"
              >清空购物车</el-button
            >
            <span class="continue-shopping" @click="$router.push('/')"
              >继续购物</span
            >
          </div>
          <div class="footer-right">
            <div class="total-info">
              已选 <span class="highlight">{{ selectedCount }}</span> 件商品
              合计：<span class="total-amount">¥{{ totalAmount }}</span>
            </div>
            <el-button
              type="danger"
              class="checkout-btn"
              :disabled="selectedCount === 0"
              @click="goToCheckout"
            >
              去结算
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <MyFoot></MyFoot>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from "vuex";
import MyFoot from "@/components/home/foot.vue";

export default {
  name: "ShoppingCar",
  components: {
    MyFoot,
  },
  data() {
    return {
      Home_list1: [
        "顺电招聘",
        "我的顺电",
        "家电医院",
        "为您服务",
        "更多精选",
        "手机顺电",
        "品牌入驻",
        "仓库招租",
      ],
    };
  },
  computed: {
    ...mapState(["cartItems"]),
    ...mapGetters(["selectedCount", "totalAmount", "allSelected"]),
  },
  methods: {
    ...mapActions([
      "removeFromCart",
      "updateQuantity",
      "toggleSelect",
      "toggleAllSelect",
      "CLEAR_CART",
    ]),

  clearCart() {
  this.$confirm('确定要清空购物车吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    this.CLEAR_CART().catch(err => {
      console.error('清空购物车失败:', err)
      this.$message.error('清空购物车失败')
    })
  })
},

    removeItem(item) {
      this.removeFromCart(item.id).then(() => {
        this.$message.success("已从购物车移除");
      });
    },
    goToCheckout() {
      if (this.selectedCount === 0) {
        this.$message.warning("请至少选择一件商品");
        return;
      }
    },
  },
};
</script>

<style scoped>
.cart-header {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  font-weight: bold;
  background: #f5f5f5;
}

.cart-header > div {
  padding: 0 10px;
}

.header-select {
  width: 80px;
  text-align: center;
}

.header-info {
  flex: 1;
}

.header-price,
.header-quantity,
.header-subtotal {
  width: 120px;
  text-align: center;
}

.header-action {
  width: 80px;
  text-align: center;
}

.cart-list {
  margin-bottom: 20px;
}

.cart-item {
  display: flex;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.item-select {
  width: 80px;
  text-align: center;
}

.item-info {
  flex: 1;
  display: flex;
  align-items: center;
}

.item-image {
  width: 80px;
  height: 80px;
  margin-right: 15px;
  object-fit: contain;
}

.item-details {
  flex: 1;
}

.item-name {
  margin: 0 0 5px 0;
  font-size: 16px;
}

.item-spec {
  margin: 0;
  color: #999;
  font-size: 12px;
}

.item-price,
.item-subtotal {
  width: 120px;
  text-align: center;
}

.item-quantity {
  width: 120px;
  text-align: center;
}

.item-action {
  width: 80px;
  text-align: center;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-top: 1px solid #eee;
}

.footer-left {
  display: flex;
  align-items: center;
}

.continue-shopping {
  margin-left: 20px;
  color: #666;
  cursor: pointer;
}

.continue-shopping:hover {
  color: #f56c6c;
}

.total-info {
  margin-right: 20px;
  font-size: 16px;
}

.highlight {
  color: #f56c6c;
  font-weight: bold;
}

.total-amount {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}

.checkout-btn {
  width: 120px;
  height: 40px;
  font-size: 16px;
}

@media (max-width: 768px) {
  .cart-header {
    display: none;
  }

  .cart-item {
    flex-wrap: wrap;
    position: relative;
    padding: 15px 0;
  }

  .item-select {
    position: absolute;
    left: 10px;
    top: 15px;
  }

  .item-info {
    padding-left: 40px;
    margin-bottom: 10px;
  }

  .item-price,
  .item-quantity,
  .item-subtotal {
    flex: 1;
    text-align: left;
    padding-left: 40px;
    margin-bottom: 10px;
  }

  .item-action {
    position: absolute;
    right: 10px;
    top: 15px;
  }

  .cart-footer {
    flex-direction: column;
  }

  .footer-right {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
  }

  .total-info {
    margin-right: 0;
    margin-bottom: 10px;
  }
}
.ShoppingCar {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.Home_list1 {
  width: 100%;
  margin-top: 0;
  background-color: #f5f5f5;
}
.Home_list1 ul {
  min-height: 30px;
  display: flex;
  flex-direction: row;
  float: right;
  margin-right: 700px;
}
.Home_list1::after {
  content: "";
  display: table;
  clear: both;
}
.Home_list1 ul li {
  list-style: none;
  padding: 10px 10px 0px 10px;
  font-size: 13px;
  line-height: 1;
  vertical-align: top;
}

.ShoppingCar_nav {
  display: flex;
  height: 100px;
  align-items: center;
  border-bottom: 1px solid #eee;
  margin: 0 auto;
  max-width: 1200px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.ShoppingCar_nav > div:first-child img {
  height: 50px;
}

.ShoppingCar_h3 {
  padding: 0 30px;
  font-size: 20px;
  flex-shrink: 0;
}

.ShoppingCar_p {
  flex: 1;
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  padding-left: 20px;
}

.cart-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 40px 20px;
  background-color: #fff;
  text-align: center;
}

.empty-cart {
  padding: 50px 0;
}

.empty-image {
  width: 200px;
  height: 200px;
  margin-bottom: 20px;
  opacity: 0.6;
}

.empty-cart p {
  font-size: 16px;
  color: #666;
  margin-bottom: 20px;
}

.btn-shopping {
  padding: 10px 30px;
  background: #f56c6c;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.btn-shopping:hover {
  background: #e05b5b;
}
</style>